package com.czk.springboot.reactive.stream;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class Demo {
    public static void main(String[] args) {
        // 提供个字符串-定义数据提供者
        Supplier<String> supplier = () -> "42";
        // 定义断言，验证是否是数字
        Predicate<String> isNumber = str -> str.matches("-?\\d+(\\.\\d+)?");
        // 转换参数，把字符串变成数字
        Function<String, Integer> change = Integer::parseInt;
        // 打印数字
        Consumer<Integer> consumer = integer -> {
            if (integer % 2 == 0) {
                System.out.println("偶数");
            } else {
                System.out.println("奇数");
            }
        };
        // 串在一起判断42字符串时奇数还是偶数
        combine(supplier, isNumber, change, consumer);
        // 函数式调用
        combine(() -> "a", str -> str.matches("-?\\d+(\\.\\d+)?"), Integer::parseInt, System.out::println);
    }

    public static void combine(Supplier<String> supplier, Predicate<String> predicate, Function<String, Integer> function, Consumer<Integer> consumer) {
        if (predicate.test(supplier.get())) {
            consumer.accept(function.apply(supplier.get()));
        } else {
            System.out.println("非法数字");
        }
    }
}
